// OpenCVApp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <cv.h>
#include <highgui.h>

int main( int argc, char** argv ) {

	char* infile="";
	char* outfile="";
	char* smooth="gaussion";
	char* unsharp="unsharp";

	for(int i=0; i<argc; i++)
	{
		if (strcmp(argv[i],"-i")==0) {
			infile=argv[i+1];
		}
		else if (strcmp(argv[i],"-o")==0) {
			outfile=argv[i+1];
		}
		else if (strcmp(argv[i],"--s")==0) {
			smooth=argv[i+1];
		}
		else if (strcmp(argv[i],"--f")==0) {
			unsharp=argv[i+1];
		}
	}	

	if(strcmp(infile,"")==0)
	{
		//print usage
		std::cout<<"\nUsage: "<<
			"OpenCVApp -i <input_file> [-o <output_file>]\n\n"
			"  -i - Input video file \n"
			"  -o - (Optional) Output video file\n" ;
	}

	else
	{

		//CvCapture* capture = cvCreateFileCapture( "E:/fiber/tmp1.avi");
		//CvCapture* capture = cvCaptureFromAVI("E:/fiber/tmp10.wmv");
		//CvCapture* capture = cvCreateCameraCapture(-1);

		CvCapture* capture = cvCreateFileCapture(infile);
	
		if(!capture){
			std::cout<<"Cannot read the input file";
			return -1;
		}

		IplImage *frame=cvQueryFrame(capture);//Init the video read

		//video writer
		CvVideoWriter *writer = 0;
	
		//frame rate fps
		double fps = cvGetCaptureProperty (
			capture,
			CV_CAP_PROP_FPS
		);

		//source video size
		CvSize size = cvSize(
			(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),
			(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT)
		);

		if(strcmp(outfile,"")!=0)
		{
			//writer=cvCreateVideoWriter("E:/fiber/out1_11_11_unsharp_6_2.avi",-1, fps, size);  
			//writer = cvCreateVideoWriter(outfile,-1,fps,size);
			writer = cvCreateVideoWriter(outfile,CV_FOURCC('M', 'J', 'P', 'G'),fps,size);
			//-1 allows you to choose video format
		}

		//Create windows
		cvNamedWindow("Input", CV_WINDOW_AUTOSIZE );

		cvNamedWindow("Intermediate (Smooth)", CV_WINDOW_AUTOSIZE );
		cvMoveWindow("Intermediate (Smooth)",size.width/2,size.height/2);

		cvNamedWindow("Output (Unsharp)", CV_WINDOW_AUTOSIZE );
		cvMoveWindow("Output (Unsharp)",size.width,0);
	
	
		double k = 0.6;				// parameters
		int k_mult = 2;
		double weight = k * k_mult;
	
		while(1) {
			frame = cvQueryFrame( capture );
		
			if( !frame ) break;

			cvShowImage("Input",frame);

			IplImage* smooth = cvCreateImage(size, frame->depth, frame->nChannels);
			IplImage* subtracted = cvCreateImage(size, frame->depth, frame->nChannels);
			IplImage* unsharp = cvCreateImage(size, frame->depth, frame->nChannels);

			cvSmooth(frame, smooth, CV_GAUSSIAN, 0, 0, 1.8);   //11 x 11

			cvShowImage("Intermediate (Smooth)",smooth);

			cvCopy(smooth, frame, NULL);     //copy smooth matrix to frame back		

			cvSmooth(frame, smooth, CV_GAUSSIAN, 0, 0, 1.8);   //apply cv smooth again
			cvSub(frame, smooth, subtracted, NULL);		  //create substract matrix	  
			cvAddWeighted(frame, 1, subtracted, weight, 0, unsharp);  //create unsharp matrix
			
			if(strcmp(outfile,"")!=0)
			{
				cvWriteFrame( writer, unsharp);    //write unsharp to file
			}

			cvShowImage("Output (Unsharp)", unsharp);

			cvReleaseImage( &smooth ); 
			cvReleaseImage( &subtracted ); 
			cvReleaseImage( &unsharp ); 

		
			char c = cvWaitKey(1);
			if( c == 27 ) break;
		
		}


		cvReleaseCapture( &capture );
		cvReleaseVideoWriter( &writer );
		cvDestroyWindow( "Input" );
		cvDestroyWindow( "Intermediate (Smooth)" );
		cvDestroyWindow( "Output (Unsharp)" );
	}
		
	return 0;
}
